/*******************************************************************************
 * Copyright (c) 2010 Mikhail Garber.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License (LGPL)
 * which accompanies this distribution
 * 
 * Contributors:
 *     Mikhail Garber - initial API and implementation
 ******************************************************************************/
package plumber.flow;

import java.util.Set;

import plumber.common.Utils;
import plumber.component.ComponentManager;
import plumber.model.Flow;
import plumber.model.FlowStep;

/**
 * default Flow Validator implementation
 * @author mgarber
 *
 */
public class DefaultFlowValidator implements FlowValidator {

	
	@Override
	/**
	 * validate that:
	 * Flow contains Steps;
	 * Flow has first Step;
	 * every Step passes its own validation;
	 * there is a matching Component for every Step 
	 */
	public void validate(Flow flow, ComponentManager finder) {
		Set<FlowStep> steps = flow.getSteps();
		Utils.validatePresentCollection(steps, "steps");
		Utils.validatePresent(flow.getFirstStep(), "first step");
		boolean foundFirstStep = false;
		for(FlowStep step : steps) {
			step.validate();
			finder.find(step.getComponent(), step.getMethod());
			if(step.equals(flow.getFirstStep())) {
				foundFirstStep = true;
			}
		}
		if(!foundFirstStep) {
			throw new IllegalStateException("could not find first step");
		}
	}

	
}
